package cc.lj.internal.coupons.domain.ticket.service.impl.useTicketRule;

import cc.lj.internal.api.ordershare.dto.OrderOnlineAPIDTO;
import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketBatchDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketMainDO;
import cc.lj.internal.coupons.domain.ticket.repository.ITicketBatchLifecycleContinueRepository;
import cc.lj.internal.coupons.domain.ticket.service.AbstractUseTicketRuleDomainService;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.TicketBatchLifecycleContinuePO;
import cc.lj.internal.coupons.interfaces.dto.command.UseTicketCheckCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 电子券用券有效期规则 验证
 * @author liangxifeng
 * @date 2023/6/25 17:02
 */
@Service("useTicketDateRuleDomainServiceImpl")
@Slf4j
public class UseTicketDateRuleDomainServiceImpl extends AbstractUseTicketRuleDomainService {
    //连续有效期
    @Resource(name = "ticketBatchLifecycleContinueRespository")
    private ITicketBatchLifecycleContinueRepository continueRepository;

    @Override
    protected void rule(TicketBatchDO batchDO, TicketMainDO ticketMainDO, UseTicketCheckCommand ticketCheckCommand, OrderOnlineAPIDTO orderOnlineAPIDTO) {
        log.info("【电子券用券验证】：券有效期验证开始==");
        //通过电子券批次主键查询连续有效期表数据
        TicketBatchLifecycleContinuePO ticketBatchLifecycleContinuePO = new TicketBatchLifecycleContinuePO();
        ticketBatchLifecycleContinuePO.setLifeBatchId(batchDO.getBatchId());
        List<TicketBatchLifecycleContinuePO> ticketBatchLifecycleContinuePOList = continueRepository.getList(ticketBatchLifecycleContinuePO);
        if(ticketBatchLifecycleContinuePOList == null || ticketBatchLifecycleContinuePOList.size() == 0) {
            throw new ServiceException(CoupousExceptionEnum.TICKET_CONTINUE_LIST_ERROR);
        }
        ticketBatchLifecycleContinuePO = ticketBatchLifecycleContinuePOList.get(0);
        //判断未到开始时间  当前日期 < 电子券开始日期
        if(LocalDate.now().compareTo( ticketBatchLifecycleContinuePO.getLifeStartDate()) < 0) {
            throw new ServiceException(CoupousExceptionEnum.TICKET_NO_STAET);
        }

        //已过期  当前日期 > 电子券截止日期
        if(LocalDate.now().compareTo( ticketBatchLifecycleContinuePO.getLifeEndDate()) > 0) {
            throw new ServiceException(CoupousExceptionEnum.TICKET_EXPIRE);
        }
        log.info("【电子券用券验证】：券有效期验证通过==");
    }
}
